package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.*;
import com.my.service.SituationAnalysisService;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户情况分析 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-09-07
 */
@Controller
@RequestMapping("/situationAnalysis")
public class SituationAnalysisController {


    @Autowired
    SituationAnalysisService situationAnalysisService;
    //创建日志Logger的对象
    Logger logger = Logger.getLogger(SituationAnalysisController.class);


    /**
     * 添加客户需求分析
     * @param param
     * @return
     */
    @RequestMapping("/insertSituationAnalysis")
    @ResponseBody
    public boolean insert(String param, HttpSession session){
        //使用反射吧json的字符串转换成实体类对象
        SituationAnalysis situationAnalysis = JSONObject.parseObject(param,SituationAnalysis.class);
        situationAnalysis.setCreateTime(new Date().getTime());
        //从session中获取用户名的id
        User user=SessionUtil.getSessionUser(session);
        situationAnalysis.setUserId(user.getId());
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        situationAnalysis.setAreaid(areaid);
        boolean flag = situationAnalysisService.insert(situationAnalysis);
        String msg =  flag?"添加成功"+situationAnalysis:"添加失败";
        logger.info(msg);
        return flag;
    }


    /**
     * 分页查询客户需求分析
     * @param
     * @param voPage
     * @return
     */
    @RequestMapping("/selectSituationAnalysis")
    @ResponseBody
    public ResultPage<SituationAnalysis> selectSituationAnalysis(String lin, String data, VoPage voPage, HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map = new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        //获取谁登陆的就是谁的id
        //User user = SessionUtil.getSessionUser(session);
        //获取到用户的id
        //User user=SessionUtil.getSessionUser(session);
        //user.getId();
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<SituationAnalysis> list = situationAnalysisService.selectMyPage(map,voPage,session,areaid,admin);
        logger.info("查询成功");
        return list;
    }





    /**
     * 每个销售每个月的总单数
     * @param lin
     * @param data
     * @param voPage
     * @param session
     * @return
     */
    @RequestMapping("/selectCount")
    @ResponseBody
    public ResultPage<SituationAnalysis> selectCount(String lin, String data, VoPage voPage, HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map = new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        //获取谁登陆的就是谁的id
        User user = SessionUtil.getSessionUser(session);
        //获取到用户的id
        user.getId();
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<SituationAnalysis> list = situationAnalysisService.selectCount(map,voPage,areaid,admin);
        logger.info("查询成功");
        return list;
    }











    /**
     * 修改客户需求分析
     * @param param
     * @return
     */
    @RequestMapping("/updateSituationAnalysis")
    @ResponseBody
    public String updateSituationAnalysis(String param){
        SituationAnalysis situationAnalysis = JSONObject.parseObject(param,SituationAnalysis.class);
        if(situationAnalysisService.updateById(situationAnalysis)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }


    /**
     * 删除客户需需求分析
     * @param id
     * @return
     */
    @RequestMapping("/deleteSituationAnalysis")
    @ResponseBody
    public boolean deleteSituationAnalysis(Long id){
        return situationAnalysisService.deleteById(id);
    }

    /**
     * 返回页面
     * @return
     */
    @RequestMapping("/situation_analysis")
    public String situationAnalysis(){
        return "situation_analysis";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/sinuation_add")
    public String sinuationAdd(){
        return "/Alert/sinuation_add";
    }

    /**
     * 返回编辑
     * @return
     */
    @RequestMapping("/sinuation_edit")
    public String sinuationEdit(){
        return "/Alert/sinuation_edit";
    }


    /**
     * 根据录入人查询
     * @param
     * @return
     */
    @RequestMapping("/selectByPerson")
    @ResponseBody
    public ResultPage<SituationAnalysis> selectyByPerson(VoPage voPage,String person,Long areaid,HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        ResultPage<SituationAnalysis> list = situationAnalysisService.selectByPerson(voPage,person,areaid,admin);
        logger.info("成功");
        return list;


    }



    /**
     * 返回月报
     * @return
     */
    @RequestMapping("/month")
    public String month(){
        return "month";
    }

    /**
     * 通过销售管理的月报弹出的明细框
     * @return
     */
    @RequestMapping("/frame")
    public String frame(String person , Model model){
        model.addAttribute("person",person);
        return "/Alert/frame";
    }





}
